package com.sise.netty.基于jboss的序列化方案;

import com.sise.netty.Gzip流应用.GzipUtil;
import com.sise.netty.基于jboss的序列化方案.dto.ResponseDto;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author idea
 * @data 2019/4/15
 */
public class ClientHandle extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ResponseDto) {
            ResponseDto responseDto = (ResponseDto) msg;
            byte[] result = responseDto.getResponseMsg();
            System.out.println("[client] get msg from server:" + new String(GzipUtil.unzip(result)));
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("client caught some unknow exception");
        Channel channel = ctx.channel();
        if (channel.isActive()) {
            ctx.close();
        }
    }
}
